package com.liy.framework.common.openoffice;

import java.net.*;
import com.das.framework.common.utils.*;
import com.artofsolving.jodconverter.openoffice.connection.*;
import java.io.*;
import com.artofsolving.jodconverter.openoffice.converter.*;
import com.artofsolving.jodconverter.*;
import com.liy.framework.common.utils.Tools;

public class OpenOfficeTool
{
    public static final String LOCAL_HOST = "10.188.10.146";
    public static final int LOCAL_PORT = 8100;
    public static DocumentFormatRegistry formatFactory;
    
    public static void main(final String[] args) {
        try {
            convert("C:\\Temp\\doc\\2.xls", "C:\\Temp\\doc\\8.pdf");
        }
        catch (ConnectException e) {
            e.printStackTrace();
        }
    }
    
    public static void convert(final String inputFilePath, final String outputFilePath) throws ConnectException {
        convert(inputFilePath, outputFilePath, "10.188.10.146", 8100);
    }
    
    public static void convert(final String inputFilePath, final String outputFilePath, final String connectIp, final int connectPort) throws ConnectException {
        if (Tools.StringIsNullOrSpace(inputFilePath) || Tools.StringIsNullOrSpace(outputFilePath) || Tools.StringIsNullOrSpace(connectIp)) {
            throw new IllegalArgumentException("\u53c2\u6570\u5f02\u5e38\uff01\uff01");
        }
        final OpenOfficeConnection connection = (OpenOfficeConnection)new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
        final DocumentConverter converter = getConverter(connectIp, connection);
        converter.convert(new File(inputFilePath), new File(outputFilePath));
        connection.disconnect();
    }
    
    public static void convert(final InputStream inputStream, final String inputFileExtension, final OutputStream outputStream, final String outputFileExtension) throws ConnectException {
        convert(inputStream, inputFileExtension, outputStream, outputFileExtension, "10.188.10.146", 8100);
    }
    
    public static void convert(final InputStream inputStream, final String inputFileExtension, final OutputStream outputStream, final String outputFileExtension, final String connectIp, final int connectPort) throws ConnectException {
        if (inputStream == null || Tools.StringIsNullOrSpace(inputFileExtension) || outputStream == null || Tools.StringIsNullOrSpace(outputFileExtension) || Tools.StringIsNullOrSpace(connectIp)) {
            throw new IllegalArgumentException("\u53c2\u6570\u5f02\u5e38\uff01\uff01");
        }
        final OpenOfficeConnection connection = (OpenOfficeConnection)new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
        final DocumentConverter converter = getConverter(connectIp, connection);
        converter.convert(inputStream, OpenOfficeTool.formatFactory.getFormatByFileExtension(inputFileExtension), outputStream, OpenOfficeTool.formatFactory.getFormatByFileExtension(outputFileExtension));
        connection.disconnect();
    }
    
    private static DocumentConverter getConverter(final String connectIp, final OpenOfficeConnection connection) {
        final DocumentConverter converter = (DocumentConverter)(("localhost".equals(connectIp) || "127.0.0.1".equals(connectIp) || "0:0:0:0:0:0:0:1".equals(connectIp)) ? new OpenOfficeDocumentConverter(connection) : new StreamOpenOfficeDocumentConverter(connection));
        return converter;
    }
    
    static {
        OpenOfficeTool.formatFactory = (DocumentFormatRegistry)new DefaultDocumentFormatRegistry();
    }
}
